package com.sunwayhorizo.health.business.clinic.emr.template.query;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;

@Name("ClinicLibraryQuery")
@Stateless
public class ClinicLibraryQuery implements ClinicLibraryQueryLocal
{
    @PersistenceContext(unitName = "sunwayhealth")
    public EntityManager em;

    @SuppressWarnings("unchecked")
    public List<Object> findClinicLibList(String conceptCode,String deptRoleid) throws Hl7Exception
    {
        StringBuilder sb = new StringBuilder();
        sb.append("select distinct d.actId, d.titleTxt from Document d , RimCoreActCdSets c , ActParticipation p ");
        sb.append("where d.actId = c.actId  ");
        if (StrUtil.isNotEmpty(conceptCode))
        {
            sb.append(" and c.conceptCode = '" + conceptCode + "'");
        }
        if (StrUtil.isNotEmpty(deptRoleid))
        {
            sb.append(" and d.actId = p.participationAct.actId and p.roleParticipation.roleId = ");
            sb.append(deptRoleid); // 科室的角色id
        }

        List<Object> docs = em.createQuery(sb.toString()).getResultList();
        return docs;
    }
    
    @SuppressWarnings("unchecked")
    public List<Object> findClinicLibListByPage(String conceptCode,String deptRoleid,int currentPage, int pageSize) throws Hl7Exception
    {
        ResultsDTO resultsDTO = new ResultsDTO();
        resultsDTO.setToPage(currentPage); // 设置跳转至
        resultsDTO.setPageSize(pageSize); // 设置每页大小

        StringBuilder sb = new StringBuilder();
        sb.append("select distinct d.actId, d.titleTxt from Document d , RimCoreActCdSets c , ActParticipation p ");
        sb.append(" where d.actId = c.actId ");
        if ( StrUtil.isNotEmpty(conceptCode))
        {
            sb.append(" and c.conceptCode = '" + conceptCode + "'");
        }
        if (StrUtil.isNotEmpty(deptRoleid))
        {
            sb.append(" and d.actId = p.participationAct.actId and p.roleParticipation.roleId = ");
            sb.append(deptRoleid); // 科室的角色id
        }

        List<Object> totaldocs = em.createQuery(sb.toString()).getResultList(); // 总共的记录
        if (ListUtil.isNotEmpty(totaldocs))
        {
            // 设置总记录数
            resultsDTO.setTotalRecords(totaldocs.size()); 
        }
        Query query = em.createQuery(sb.toString());
        query.setFirstResult(resultsDTO.getBeginIndex());
        query.setMaxResults(resultsDTO.getPageSize());
        List<Object> docs = query.getResultList();
        return docs;
    }
    
    @SuppressWarnings("rawtypes")
    public List findActIdByName(String name) throws Hl7Exception
    {
        StringBuilder jbql = new StringBuilder();
        jbql.append(" select d.actId from Document d where d.titleTxt='" + name + "' and d.codeEtsId='CLINIClIB'");
        List list = em.createQuery(jbql.toString()).getResultList();
        return list;
    }
}
